﻿
$(function () {
    //初始化列表信息
    init();
});

//初始化列表信息
function init(queryData) {

    $('#department_tree').tree({
        animate: true,
        url: '/Department/GetTreeList',
        onClick: function (node) {
            var querData = {
                j_depatmentID: node.id
            };
            GetList(querData);
        }
    });
    GetList();
}

function GetList(queryData) {
    $('#list').datagrid({
        //title: '用户列表',
        idField: 'UserID',
        fitColumns: 'true',
        singleSelect: true,
        autoRowHeight: true,
        nowrap: true,
        striped: true,
        pagination: true,
        url: 'User/GetList',
        queryParams: queryData,    //参数为了多条件查询预留
        columns: [[
			{ field: 'ck', checkbox: true },
			{ field: 'UserID', title: 'ID', hidden: true },
			{ field: 'UserName', title: '账号', width: 180 },
			{ field: 'EmployeeName', title: '姓名', width: 180 },
			{ field: 'DepartmentID', title: '部门', width: 180 },
			{ field: 'RoleID', title: '角色（职务）', width: 180 },
			{ field: 'Telephone', title: '固定电话', width: 180 }
        ]],
    });
}
//弹出增加窗口
function add() {
    $('#dlg').dialog('open').dialog('setTitle', '增加');
    $('#fm').form('clear');
    var node = $('#department_tree').tree('getSelected');
    if (node) {
        $('#DepartmentParentID').val(node.id);//用于点击添加的时候给隐藏域以及父级部门赋值
        $('#comb_DepartmentID').combotree('setValue', node.id);
    }
    $('#EmployeeSex').combobox('setValue', 0);
    url = '/User/Create';
}

//弹出修改窗口,绑定数据
function edit() {
    var rows = $("#list").datagrid("getSelections");
    if (rows.length == 1) {
        var updateID = rows[0].UserID;
        var myDate = new Date().getTime();//防止数据缓存
        $.getJSON("/User/GetEntity?date="+myDate, { ID: updateID }, function (data) {
            //绑定数据显示到用户控件上面
            $('#fm').form('load', data);
            $('#comb_DepartmentID').combotree('setValue', data.DepartmentID);
            $('#comb_SignConfig').combobox('setValue', data.AttendanceConfigID);
            var date = formatDate(data.Birthday, "yyyy-MM-dd");
            $("#Birthday").datebox("setValue", date)
        })
        $('#dlg').dialog('open').dialog('setTitle', '修改');
        url = '/User/Edit';
    }
    else {
        $.messager.alert("友情提示", "每次只能修改一行数据，你已经选择了<font color='red' size='6'>" + UpdateDictionaryInfoID.length + "</font>行");
    }
}

//保存数据
function save() {
    $('#fm').form('submit', {
        url: url,
        onSubmit: function () {
            return $(this).form('validate');
        },
        success: function (result) {
            if (result == 'OK') {
                $('#dlg').dialog('close');      // close the dialog  
                $('#list').datagrid('reload');    // reload the user data  
            } else {
                $.messager.show({
                    title: 'Error',
                    msg: result
                });
            }
        }
    });
}

//删除
function del() {
    var rows = $('#list').datagrid('getSelected');
    if (rows) {
        $.messager.confirm(delDlgTitle, delDlgMsg, function (r) {
            if (r) {
                $.post('/User/Delete', { id: rows.UserID },
                    function (result) {
                        if (result == 'OK') {
                            $("#list").datagrid('clearSelections');
                            $('#list').datagrid('reload');    // reload the user data  
                        } else {
                            $.messager.show({   // show error message  
                                title: 'Error',
                                msg: result
                            });
                        }
                    });
            }
        });
    }
}